package com.pussinboots.morning.cms.modules.system.category.service.impl;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.pussinboots.morning.cms.modules.system.category.dto.OsCategoryDto;
import com.pussinboots.morning.cms.modules.system.category.entity.OsCategory;
import com.pussinboots.morning.cms.modules.system.category.mapper.OsCategoryMapper;
import com.pussinboots.morning.cms.modules.system.category.service.IOsCategoryService;
import com.pussinboots.morning.cms.modules.system.product.entity.OsProductCategory;
import com.pussinboots.morning.cms.modules.system.product.mapper.OsProductCategoryMapper;
import com.pussinboots.morning.cms.modules.system.product.mapper.OsProductMapper;
import com.pussinboots.morning.common.model.PageInfo;

/**
 * <p>
 * 分类表 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2017-04-15
 */
@Service
public class OsCategoryServiceImpl extends ServiceImpl<OsCategoryMapper, OsCategory> implements IOsCategoryService {
	
	@Autowired
	private OsCategoryMapper categoryMapper;
	
	@Autowired
	private OsProductMapper productMapper;
	
	@Autowired
	private OsProductCategoryMapper productCategoryMapper;
	
	
	@Override
	public PageInfo selectByPullPage(PageInfo pageInfo) {

		//设置分页
		Page<OsCategory> page = new Page<>(pageInfo.getNowpage(), pageInfo.getSize());
		
		//设置排序
		String sortStr = pageInfo.getSort();
		
		//查询数据
		List<OsCategory> osCategoryList = null;
		if(sortStr!=null && !sortStr.equals("")){
			osCategoryList = categoryMapper.selectPage(page, new EntityWrapper<OsCategory>().orderBy(sortStr, false));
		}else{
			osCategoryList = categoryMapper.selectPage(page, null);
		}
		
		//设置返回对象
        pageInfo.setTotal(page.getTotal());
        pageInfo.setRows(osCategoryList);
        
        //返回数据
		return pageInfo;
	}


	@Override
	public List<OsCategoryDto> selectCategoryAndNumber(Integer parentId) {
		EntityWrapper<OsCategory> wrapper = new EntityWrapper<OsCategory>();
		wrapper.where("parent_id={0}",parentId); 
		List<OsCategory> osCategoryList = categoryMapper.selectList(wrapper);
		
		
		List<OsCategoryDto> categoryDtoList = new ArrayList<OsCategoryDto>();
		for(OsCategory category : osCategoryList){
			OsCategoryDto categoryDto = new OsCategoryDto();
			try {
				PropertyUtils.copyProperties(categoryDto, category);
				Integer count = productCategoryMapper.selectCount(new EntityWrapper<OsProductCategory>().where("category_id={0}",category.getCategoryId()));
				categoryDto.setNumber(count);
				categoryDtoList.add(categoryDto);
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				e.printStackTrace();
			}
		}
	
		return categoryDtoList;
	}
	
	
	
	
}
